package com.rescue.controller.admin;

import com.rescue.forum.dto.ArticlePageQueryDTO;
import com.rescue.forum.dto.UpdateBoardDTO;
import com.rescue.forum.vo.AdminGetAttachmentVO;
import com.rescue.result.PageResult;
import com.rescue.result.Result;
import com.rescue.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/forum/article")
@RestController
@Api(tags = " 文章相关接口")
@Slf4j
public class ArticleController {
    @Autowired
    private ArticleService articleService;

     /**
     *分页查询
     * @param articlePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页条件查询文章")
    private Result<PageResult> list(ArticlePageQueryDTO articlePageQueryDTO) {
        log.info("分页条件查询文章");
        PageResult pageResult = articleService.page(articlePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id删除
     * @param articleId
     * @return
     */
    @PutMapping("/delete")
    @ApiOperation("根据id删除")
    private Result deleteById(String articleId){
        articleService.deleteById(articleId);
        return Result.success();
    }

    /**
     * 修改文章板块
     * @param updateBoardDTO
     * @return
     */
    @ApiOperation("修改文章板块")
    @PutMapping("/updateBoard")
    private Result updateBoard(@RequestBody UpdateBoardDTO updateBoardDTO){
        articleService.updateBoard(updateBoardDTO);
        return Result.success();
    }

    /**
     * 根据文章Id获取附件
     * @param articleId
     * @return
     */
    @ApiOperation("根据文章Id获取附件")
    @GetMapping("/getAttachment")
    private Result<List<AdminGetAttachmentVO>> getAttachment(String articleId){
        List<AdminGetAttachmentVO> list = articleService.getAttachment(articleId);
        return Result.success(list);
    }

    /**
     * 置顶或取消置顶
     * @param articleId
     * @param topType
     * @return
     */
    @ApiOperation("置顶或取消置顶")
    @PostMapping("/top")
    private Result topArticle(String articleId,Integer topType){
        log.info("{}id为{}的文章",topType==1?"置顶":"取消",articleId);
        articleService.topArticle(articleId,topType);
        return Result.success();
    }
    @ApiOperation("审核文章")
    @PostMapping("/audit")
    private Result auditArticle(String articleId){
        log.info("审核文章{}",articleId);
        articleService.auditArticle(articleId);
        return Result.success();
    }
}
